package com.woniuxy.joylifeforum.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.common.entity.*;
import com.woniuxy.common.mapper.*;
import com.woniuxy.joylifeforum.dto.TopicDto;
import com.woniuxy.joylifeforum.service.TopicService;
import com.woniuxy.joylifeforum.utils.ForumRedis;
import com.woniuxy.joylifeforum.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    private ForumTopicMapper forumTopicMapper;
    @Autowired
    private ForumReplyMapper forumReplyMapper;
    @Autowired
    private CenterUserMapper centerUserMapper;
    @Autowired
    private ForumForumMapper forumForumMapper;
    @Autowired
    private ForumConcernMapper forumConcernMapper;
    @Autowired
    private ForumTopicPraiseMapper forumTopicPraiseMapper;
    @Autowired
    private ForumRedis forumRedis;
    @Autowired
    private ForumPhotoMapper forumPhotoMapper;
    @Autowired
    private ForumTopicCollectMapper forumTopicCollectMapper;
    @Autowired
    private ForumExpMapper forumExpMapper;
    @Autowired
    private ForumReplyPraiseMapper ForumReplyPraiseMapper;
    @Autowired
    private ForumTopicReportMapper forumTopicReportMapper;
    @Autowired
    private ForumReplyReportMapper forumReplyReportMapper;
    @Autowired
    private ForumAdminMapper forumAdminMapper;
    @Autowired
    private RedisUtil redisUtil;
    //查询首页最新
    @Override
    public PageInfo<TopicDto> findTopic(String userId,Integer page, Integer limit) {
        //查询最新话题
        ForumTopicExample topicExample = new ForumTopicExample();
        //时间排序,状态正常话题
        topicExample.setOrderByClause("date DESC");
        topicExample.createCriteria().andStatusEqualTo(0);
        //分页
        PageHelper.startPage(page, limit);
        //查询话题
        List<ForumTopic> forumTopics = forumTopicMapper.selectByExampleWithBLOBs(topicExample);
        //遍历封装赋值
        List<TopicDto> TopicDtos = new ArrayList<>();
        for (ForumTopic forumTopic : forumTopics) {
            //用户赋值
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumTopic.getUserId());
            //社区赋值
            ForumForum forumForum = forumForumMapper.selectByPrimaryKey(forumTopic.getForumId());
            //图片赋值
            ForumPhotoExample photoExample = new ForumPhotoExample();
            ForumPhotoExample.Criteria criteria1 = photoExample.createCriteria();
            criteria1.andTopicIdEqualTo(forumTopic.getId());
            criteria1.andReplyIdIsNull();
            List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
            //社区用户数量
            long forumUserNum = forumRedis.findForumUserNum(forumTopic.getForumId());
            //话题点赞数量
            long topicPraiseNum = forumRedis.findTopicPraiseNum(forumTopic.getId());
            //判断本用户是否已点赞
            ForumTopicPraiseExample topicPraise=new ForumTopicPraiseExample();
            ForumTopicPraiseExample.Criteria criteria3 = topicPraise.createCriteria();
            criteria3.andTopicIdEqualTo(forumTopic.getId());
            criteria3.andUserIdEqualTo(userId);
            int praiseStatus = forumTopicPraiseMapper.selectByExample(topicPraise).size();
            //话题评论数
            long replyNum = forumRedis.findReplyNum(forumTopic.getId());
            //赋值
            TopicDtos.add(new TopicDto(centerUser, forumForum, forumUserNum, forumTopic,
                    topicPraiseNum, replyNum, forumPhotos,praiseStatus));
        }
        PageInfo<TopicDto> TopicDtoPH = new PageInfo<>(TopicDtos);
        return TopicDtoPH;
    }

    //我关注的社区动态
    @Override
    public PageInfo<TopicDto> findTopic(Integer page, Integer limit, String userId) {
        //查询我的关注
        ForumConcernExample concernExample = new ForumConcernExample();
        concernExample.createCriteria().andUserIdEqualTo(userId);
        List<ForumConcern> forumConcerns = forumConcernMapper.selectByExample(concernExample);
        //获取关注社区id，便于检索
        List<String> forumStr = new ArrayList<>();
        forumStr.add("");
        for (ForumConcern forumConcern : forumConcerns) {
            forumStr.add(forumConcern.getForumId());
        }
        //查询最新话题
        ForumTopicExample topicExample = new ForumTopicExample();
        //时间排序,状态正常话题
        topicExample.setOrderByClause("date DESC");
        ForumTopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andStatusEqualTo(0);
        criteria.andForumIdIn(forumStr);
        //分页
        PageHelper.startPage(page, limit);
        //查询话题
        List<ForumTopic> forumTopics = forumTopicMapper.selectByExampleWithBLOBs(topicExample);
        //遍历封装赋值
        List<TopicDto> TopicDtos = new ArrayList<>();
        for (ForumTopic forumTopic : forumTopics) {
            //用户赋值
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumTopic.getUserId());
            //社区赋值
            ForumForum forumForum = forumForumMapper.selectByPrimaryKey(forumTopic.getForumId());
            //图片赋值
            ForumPhotoExample photoExample = new ForumPhotoExample();
            ForumPhotoExample.Criteria criteria1 = photoExample.createCriteria();
            criteria1.andTopicIdEqualTo(forumTopic.getId());
            criteria1.andReplyIdIsNull();
            List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
            //社区用户数量
            long forumUserNum = forumRedis.findForumUserNum(forumTopic.getForumId());
            //话题点赞数量
            long topicPraiseNum = forumRedis.findTopicPraiseNum(forumTopic.getId());
            //判断本用户是否已点赞
            ForumTopicPraiseExample topicPraise=new ForumTopicPraiseExample();
            ForumTopicPraiseExample.Criteria criteria3 = topicPraise.createCriteria();
            criteria3.andTopicIdEqualTo(forumTopic.getId());
            criteria3.andUserIdEqualTo(userId);
            int praiseStatus = forumTopicPraiseMapper.selectByExample(topicPraise).size();
            //话题评论数
            long replyNum = forumRedis.findReplyNum(forumTopic.getId());
            //赋值
            TopicDtos.add(new TopicDto(centerUser, forumForum, forumUserNum, forumTopic,
                    topicPraiseNum, replyNum, forumPhotos,praiseStatus));
        }
        PageInfo<TopicDto> TopicDtoPH = new PageInfo<>(TopicDtos);
        return TopicDtoPH;
    }

    //我的社区动态
    @Override
    public PageInfo<TopicDto> findMyForumTopic(Integer page, Integer limit, String userId) {
        //查询我的社区
        String forumId = centerUserMapper.selectByPrimaryKey(userId).getForumId();
        //判断是否绑定社区
        if (forumId == null) {
            throw new RuntimeException("尚未认证社区");
        }
        //查询最新话题
        ForumTopicExample topicExample = new ForumTopicExample();
        //时间排序,状态正常话题
        topicExample.setOrderByClause("date DESC");
        ForumTopicExample.Criteria criteria = topicExample.createCriteria();
        criteria.andStatusEqualTo(0);
        criteria.andForumIdEqualTo(forumId);
        //分页
        PageHelper.startPage(page, limit);
        //查询话题
        List<ForumTopic> forumTopics = forumTopicMapper.selectByExampleWithBLOBs(topicExample);
        //遍历封装赋值
        List<TopicDto> TopicDtos = new ArrayList<>();
        for (ForumTopic forumTopic : forumTopics) {
            //用户赋值
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumTopic.getUserId());
            //社区赋值
            ForumForum forumForum = forumForumMapper.selectByPrimaryKey(forumTopic.getForumId());
            //图片赋值
            ForumPhotoExample photoExample = new ForumPhotoExample();
            ForumPhotoExample.Criteria criteria1 = photoExample.createCriteria();
            criteria1.andTopicIdEqualTo(forumTopic.getId());
            criteria1.andReplyIdIsNull();
            List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
            //社区用户数量
            long forumUserNum = forumRedis.findForumUserNum(forumTopic.getForumId());
            //话题点赞数量
            long topicPraiseNum = forumRedis.findTopicPraiseNum(forumTopic.getId());
            //判断本用户是否已点赞
            ForumTopicPraiseExample topicPraise=new ForumTopicPraiseExample();
            ForumTopicPraiseExample.Criteria criteria3 = topicPraise.createCriteria();
            criteria3.andTopicIdEqualTo(forumTopic.getId());
            criteria3.andUserIdEqualTo(userId);
            int praiseStatus = forumTopicPraiseMapper.selectByExample(topicPraise).size();
            //话题评论数
            long replyNum = forumRedis.findReplyNum(forumTopic.getId());
            //赋值
            TopicDtos.add(new TopicDto(centerUser, forumForum, forumUserNum, forumTopic,
                    topicPraiseNum, replyNum, forumPhotos,praiseStatus));
        }
        PageInfo<TopicDto> TopicDtoPH = new PageInfo<>(TopicDtos);
        return TopicDtoPH;
    }

    //模糊搜索
    @Override
    public PageInfo<TopicDto> findTopic(String userId,String find, Integer page, Integer limit) {
        //分页
        PageHelper.startPage(page, limit);
        //查询话题
        List<ForumTopic> forumTopics = forumTopicMapper.selectByLike(find);
        //遍历封装赋值
        List<TopicDto> TopicDtos = new ArrayList<>();
        for (ForumTopic forumTopic : forumTopics) {
            //用户赋值
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumTopic.getUserId());
            //社区赋值
            ForumForum forumForum = forumForumMapper.selectByPrimaryKey(forumTopic.getForumId());
            //图片赋值
            ForumPhotoExample photoExample = new ForumPhotoExample();
            ForumPhotoExample.Criteria criteria = photoExample.createCriteria();
            criteria.andTopicIdEqualTo(forumTopic.getId());
            criteria.andReplyIdIsNull();
            List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
            //社区用户数量
            long forumUserNum = forumRedis.findForumUserNum(forumTopic.getForumId());
            //话题点赞数量
            long topicPraiseNum = forumRedis.findTopicPraiseNum(forumTopic.getId());
            //判断本用户是否已点赞
            ForumTopicPraiseExample topicPraise=new ForumTopicPraiseExample();
            ForumTopicPraiseExample.Criteria criteria3 = topicPraise.createCriteria();
            criteria3.andTopicIdEqualTo(forumTopic.getId());
            criteria3.andUserIdEqualTo(userId);
            int praiseStatus = forumTopicPraiseMapper.selectByExample(topicPraise).size();
            //话题评论数
            long replyNum = forumRedis.findReplyNum(forumTopic.getId());
            //赋值
            TopicDtos.add(new TopicDto(centerUser, forumForum, forumUserNum, forumTopic,
                    topicPraiseNum, replyNum, forumPhotos,praiseStatus));
        }
        PageInfo<TopicDto> TopicDtoPH = new PageInfo<>(TopicDtos);
        return TopicDtoPH;
    }

    /*
    获取话题详情
     */
    @Override
    public Map<String, Object> findTopicCont(String topicId,String userId) {
        //获取话题信息
        ForumTopic forumTopic = forumTopicMapper.selectByPrimaryKey(topicId);
        //获取用户信息
        CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumTopic.getUserId());
        //获取社区信息
        ForumForum forumForum = forumForumMapper.selectByPrimaryKey(forumTopic.getForumId());
        //获取照片信息
        ForumPhotoExample photoExample = new ForumPhotoExample();
        ForumPhotoExample.Criteria criteria = photoExample.createCriteria();
        criteria.andTopicIdEqualTo(topicId);
        criteria.andReplyIdIsNull();
        List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
        //点赞量
        long topicPraiseNum = forumRedis.findTopicPraiseNum(topicId);
        //判断本用户是否已点赞
        ForumTopicPraiseExample topicPraise=new ForumTopicPraiseExample();
        ForumTopicPraiseExample.Criteria criteria3 = topicPraise.createCriteria();
        criteria3.andTopicIdEqualTo(topicId);
        criteria3.andUserIdEqualTo(userId);
        int praiseStatus = forumTopicPraiseMapper.selectByExample(topicPraise).size();
        //回复量
        long replyNum = forumRedis.findReplyNum(topicId);
        //收藏量
        ForumTopicCollectExample collectExample = new ForumTopicCollectExample();
        collectExample.createCriteria().andTopicIdEqualTo(topicId);
        long collertNum = forumTopicCollectMapper.countByExample(collectExample);
        //判断本人是否收藏
        ForumTopicCollectExample topicCollect=new ForumTopicCollectExample();
        ForumTopicCollectExample.Criteria criteria4 = topicCollect.createCriteria();
        criteria4.andTopicIdEqualTo(topicId);
        criteria4.andUserIdEqualTo(userId);
        int collectStatus = forumTopicCollectMapper.selectByExample(topicCollect).size();
        //用户经验
        //判断是否关注
        ForumConcernExample concernExample = new ForumConcernExample();
        ForumConcernExample.Criteria criteria2 = concernExample.createCriteria();
        criteria2.andUserIdEqualTo(forumTopic.getUserId());
        criteria2.andForumIdEqualTo(forumTopic.getForumId());
        List<ForumConcern> forumConcerns = forumConcernMapper.selectByExample(concernExample);
        Integer exp = 0;
        if (forumConcerns.size() == 1) {
            //有关注，查询经验
            ForumExpExample expExample = new ForumExpExample();
            ForumExpExample.Criteria criteria1 = expExample.createCriteria();
            criteria1.andForumIdEqualTo(forumTopic.getForumId());
            criteria1.andUserIdEqualTo(forumTopic.getUserId());
            exp = forumExpMapper.selectByExample(expExample).get(0).getExp();
        }
        //保存
        Map<String, Object> data = new HashMap<>();
        data.put("topic", forumTopic);
        data.put("user", centerUser);
        data.put("forum", forumForum);
        data.put("imgs", forumPhotos);
        data.put("topicPraiseNum", topicPraiseNum);
        data.put("praiseStatus", praiseStatus);
        data.put("replyNum", replyNum);
        data.put("collertNum", collertNum);
        data.put("collectStatus", collectStatus);
        data.put("exp", exp);
        return data;
    }

    /*
    查看话题回复
     */
    @Override
    public PageInfo<Map<String, Object>> findReplyCont(String userId,Integer page, Integer limit, String topicId) {
        ForumReplyExample replyExample = new ForumReplyExample();
        //最新
        replyExample.setOrderByClause("date DESC");
        ForumReplyExample.Criteria criteria = replyExample.createCriteria();
        //指定话题id
        criteria.andTopicIdEqualTo(topicId);
        //排除楼层回复
        criteria.andReplyIdIsNull();
        //排除被举报的
        criteria.andStatusEqualTo(0);
        //分页
        PageHelper.startPage(page, limit);
        List<ForumReply> forumReplies = forumReplyMapper.selectByExampleWithBLOBs(replyExample);
        //遍历回复楼层赋值
        List<Map<String, Object>> data = new ArrayList<>();
        for (ForumReply forumReply : forumReplies) {
            //用户信息
            CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumReply.getUserId());
            //用户经验
            //判断是否关注
            ForumConcernExample concernExample = new ForumConcernExample();
            ForumConcernExample.Criteria criteria2 = concernExample.createCriteria();
            criteria2.andUserIdEqualTo(forumReply.getUserId());
            criteria2.andForumIdEqualTo(forumReply.getForumId());
            List<ForumConcern> forumConcerns = forumConcernMapper.selectByExample(concernExample);
            Integer exp = 0;
            if (forumConcerns.size() == 1) {
                //有关注，查询经验
                ForumExpExample expExample = new ForumExpExample();
                ForumExpExample.Criteria criteria1 = expExample.createCriteria();
                criteria1.andForumIdEqualTo(forumReply.getForumId());
                criteria1.andUserIdEqualTo(forumReply.getUserId());
                exp = forumExpMapper.selectByExample(expExample).get(0).getExp();
            }
            //获取照片信息
            ForumPhotoExample photoExample = new ForumPhotoExample();
            ForumPhotoExample.Criteria criteria3 = photoExample.createCriteria();
            criteria3.andTopicIdEqualTo(topicId);
            criteria3.andReplyIdEqualTo(forumReply.getId());
            List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
            //点赞量
            ForumReplyPraiseExample replyPraiseExample = new ForumReplyPraiseExample();
            replyPraiseExample.createCriteria().andReplyIdEqualTo(forumReply.getId());
            long replyPraiseNum = ForumReplyPraiseMapper.countByExample(replyPraiseExample);
            //判断本用户是否已点赞
            ForumReplyPraiseExample replyPraise=new ForumReplyPraiseExample();
            ForumReplyPraiseExample.Criteria criteria4 = replyPraise.createCriteria();
            criteria4.andUserIdEqualTo(userId);
            criteria4.andReplyIdEqualTo(forumReply.getId());
            int praiseStatus = ForumReplyPraiseMapper.selectByExample(replyPraise).size();
            //回复量
            ForumReplyExample replyExample2 = new ForumReplyExample();
            ForumReplyExample.Criteria criteria1 = replyExample2.createCriteria();
            criteria1.andReplyIdEqualTo(forumReply.getId());
            long replyNum = forumReplyMapper.countByExample(replyExample2);
            Map<String, Object> map = new HashMap<>();
            map.put("forumReply", forumReply);
            map.put("centerUser", centerUser);
            map.put("exp", exp);
            map.put("forumPhotos", forumPhotos);
            map.put("replyPraiseNum", replyPraiseNum);
            map.put("praiseStatus", praiseStatus);
            map.put("replyNum", replyNum);
            data.add(map);
        }
        PageInfo<Map<String, Object>> dataHP = new PageInfo<>(data);
        return dataHP;
    }

    /*
    用户回复
     */
    @Override
    @Transactional
    public String addReplyService(String[] imgs, String topicId, String replyId, String content, String userId, String replyUserId) {
        try {
            //社区id
            ForumTopic forumTopic = forumTopicMapper.selectByPrimaryKey(topicId);
            String forumId = forumTopic.getForumId();
            //被回复用户id
            if (replyUserId == null) {
                //针对题主的回复，获取题主id
                replyUserId = forumTopic.getUserId();
                //回复量+1
                Object temp = redisUtil.get("replyNum"+topicId);
                if(temp!=null) {
                    int num = Integer.parseInt(temp.toString());
                    redisUtil.set("replyNum" + topicId, num + 1);
                }
            }
            //添加回复信息
            String newReplyId = UUID.randomUUID().toString();
            Integer isRead=1;
            if(userId.equals(replyUserId)){
                //自己回复自己，默认已读
                isRead=0;
            }
            ForumReply reply = new ForumReply(newReplyId, topicId, replyId, userId, replyUserId, forumId, 0, new Date(), isRead, content);
            forumReplyMapper.insert(reply);
            //保存图片
            if (imgs!=null&&imgs.length!=0) {
                for (String src:imgs) {
                    String imgId = UUID.randomUUID().toString();
                    ForumPhoto photo = new ForumPhoto(imgId, topicId, newReplyId, src);
                    forumPhotoMapper.insert(photo);
                }
            }
            //用户经验
            //判断是否关注
            ForumConcernExample concernExample = new ForumConcernExample();
            ForumConcernExample.Criteria criteria2 = concernExample.createCriteria();
            criteria2.andUserIdEqualTo(userId);
            criteria2.andForumIdEqualTo(forumId);
            List<ForumConcern> forumConcerns = forumConcernMapper.selectByExample(concernExample);
            if (forumConcerns.size() == 1) {
                //已关注，经验+3
                ForumExpExample expExample = new ForumExpExample();
                ForumExpExample.Criteria criteria1 = expExample.createCriteria();
                criteria1.andUserIdEqualTo(userId);
                criteria1.andForumIdEqualTo(forumId);
                //获取经验表
                ForumExp forumExp = forumExpMapper.selectByExample(expExample).get(0);
                //经验+3后保存
                forumExp.setExp(forumExp.getExp() + 3);
                forumExpMapper.updateByPrimaryKey(forumExp);
                return "1";
            }
            return "2";
        } catch (Exception e) {
            e.printStackTrace();
            return "0";
        }
    }
    /*
    回复点赞状态更改
     */
    @Override
    public Integer alterPraiseService(String userId, String replyId) {
        //判断是否已点赞
        ForumReplyPraiseExample replyPraise=new ForumReplyPraiseExample();
        ForumReplyPraiseExample.Criteria criteria = replyPraise.createCriteria();
        criteria.andReplyIdEqualTo(replyId);
        criteria.andUserIdEqualTo(userId);
        List<ForumReplyPraise> forumReplyPraises = ForumReplyPraiseMapper.selectByExample(replyPraise);
        if(forumReplyPraises.size()>0){
            //已点赞，取消赞
            ForumReplyPraiseMapper.deleteByPrimaryKey(forumReplyPraises.get(0).getId());
            return 0;
        }else{
            //未点赞，添加赞
            String id = UUID.randomUUID().toString();
            //获取被回复用户id
            String replyUserId = forumReplyMapper.selectByPrimaryKey(replyId).getUserId();
            //判断是不是自己跟自己赞者玩儿
            Integer status=1;
            if(replyUserId.equals(userId)){
            //是一个用户，默认已读
                status=0;
            }
            ForumReplyPraise forumReplyPraise=new ForumReplyPraise(id,replyId,userId,replyUserId,new Date(),status);
            ForumReplyPraiseMapper.insert(forumReplyPraise);
            return 1;
        }
    }
    /*
    话题点赞状态更改
     */
    @Override
    public Integer alterTopicPraiseService(String userId,String topicId) {
        //判断是否已点赞
        ForumTopicPraiseExample topicPraise=new ForumTopicPraiseExample();
        ForumTopicPraiseExample.Criteria criteria1 = topicPraise.createCriteria();
        criteria1.andUserIdEqualTo(userId);
        criteria1.andTopicIdEqualTo(topicId);
        List<ForumTopicPraise> forumTopicPraises = forumTopicPraiseMapper.selectByExample(topicPraise);
        if(forumTopicPraises.size()>0){
            //已点赞，取消赞
            forumTopicPraiseMapper.deleteByPrimaryKey(forumTopicPraises.get(0).getId());
            Integer topicNum = Integer.parseInt(redisUtil.get("topicPraiseNum" + topicId).toString());
            redisUtil.set("topicPraiseNum"+topicId,(topicNum-1));
            return 0;
        }else{
            //未点赞，添加赞
            String id = UUID.randomUUID().toString();
            //获取被回复用户id
            String topicUserId = forumTopicMapper.selectByPrimaryKey(topicId).getUserId();
            //判断是不是自己跟自己赞者玩儿
            Integer status=1;
            if(topicUserId.equals(userId)){
                //是一个用户，默认已读
                status=0;
            }
            ForumTopicPraise topicPraiseEntity=new ForumTopicPraise(id,topicId,userId,topicUserId,new Date(),status);
            forumTopicPraiseMapper.insert(topicPraiseEntity);
            Integer topicNum = Integer.parseInt(redisUtil.get("topicPraiseNum" + topicId).toString());
            redisUtil.set("topicPraiseNum"+topicId,(topicNum+1));
            return 1;
        }
    }
    /*
    收藏状态更改
     */
    @Override
    public Integer alterCollectTopicService(String userId, String topicId) {
        //判断是否已收藏
        ForumTopicCollectExample topicCollect=new ForumTopicCollectExample();
        ForumTopicCollectExample.Criteria criteria = topicCollect.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andTopicIdEqualTo(topicId);
        List<ForumTopicCollect> forumTopicCollects = forumTopicCollectMapper.selectByExample(topicCollect);
        if(forumTopicCollects.size()>0){
            //已收藏，删除
            forumTopicCollectMapper.deleteByPrimaryKey(forumTopicCollects.get(0).getId());
            return 0;
        }else{
            //添加收藏
            String id = UUID.randomUUID().toString();
            //获取被收藏用户id
            String topicUserId = forumTopicMapper.selectByPrimaryKey(topicId).getUserId();
            //判断是不是自恋的自己收藏自己
            Integer status=1;
            if(topicUserId.equals(userId)){
                //是一个用户，默认已读
                status=0;
            }
            ForumTopicCollect topicCollectEntity=new ForumTopicCollect(id,userId,topicId,status);
            forumTopicCollectMapper.insert(topicCollectEntity);
            return 1;
        }
    }
    /*
    楼层回复信息
     */
    @Override
    public Map<String, Object> findReplyService(String userId, String replyId) {

        //获取楼层信息
        ForumReply forumReply = forumReplyMapper.selectByPrimaryKey(replyId);
        //楼层照片
        ForumPhotoExample photoExample=new ForumPhotoExample();
        ForumPhotoExample.Criteria criteria = photoExample.createCriteria();
        criteria.andReplyIdEqualTo(replyId);
        List<ForumPhoto> forumPhotos = forumPhotoMapper.selectByExample(photoExample);
        //楼主信息
        CenterUser centerUser = centerUserMapper.selectByPrimaryKey(forumReply.getUserId());
        //楼主经验
        //判断是否关注
        ForumConcernExample concernExample = new ForumConcernExample();
        ForumConcernExample.Criteria criteria2 = concernExample.createCriteria();
        criteria2.andUserIdEqualTo(forumReply.getUserId());
        criteria2.andForumIdEqualTo(forumReply.getForumId());
        List<ForumConcern> forumConcerns = forumConcernMapper.selectByExample(concernExample);
        Integer exp = 0;
        if (forumConcerns.size() == 1) {
            //有关注，查询经验
            ForumExpExample expExample = new ForumExpExample();
            ForumExpExample.Criteria criteria1 = expExample.createCriteria();
            criteria1.andForumIdEqualTo(forumReply.getForumId());
            criteria1.andUserIdEqualTo(forumReply.getUserId());
            exp = forumExpMapper.selectByExample(expExample).get(0).getExp();
        }
        //保存
        Map<String,Object>floorUser=new HashMap<>();
        floorUser.put("forumReply",forumReply);
        floorUser.put("forumPhotos",forumPhotos);
        floorUser.put("centerUser",centerUser);
        floorUser.put("exp",exp);

        //获取回复信息
        ForumReplyExample reply=new ForumReplyExample();
        reply.setOrderByClause("date ASC");
        ForumReplyExample.Criteria criteria1 = reply.createCriteria();
        criteria1.andReplyIdEqualTo(replyId);
        criteria1.andStatusEqualTo(0);
        List<ForumReply> forumReplies = forumReplyMapper.selectByExampleWithBLOBs(reply);
        //遍历赋值
        List<Map<String,Object>>replyReply=new ArrayList<>();
        for(ForumReply forumReply1:forumReplies){
            //用户赋值
            CenterUser centerUser1 = centerUserMapper.selectByPrimaryKey(forumReply1.getUserId());
            //被回复用户赋值
            CenterUser receptionUser=centerUserMapper.selectByPrimaryKey(forumReply1.getReplyUserId());
            //经验赋值
            //判断是否关注
            ForumConcernExample concernExample3 = new ForumConcernExample();
            ForumConcernExample.Criteria criteria3 = concernExample3.createCriteria();
            criteria3.andUserIdEqualTo(forumReply1.getUserId());
            criteria3.andForumIdEqualTo(forumReply.getForumId());
            List<ForumConcern> forumConcerns1 = forumConcernMapper.selectByExample(concernExample3);
            Integer exp1 = 0;
            if (forumConcerns1.size() == 1) {
                //有关注，查询经验
                ForumExpExample expExample2 = new ForumExpExample();
                ForumExpExample.Criteria criteria4 = expExample2.createCriteria();
                criteria4.andForumIdEqualTo(forumReply.getForumId());
                criteria4.andUserIdEqualTo(forumReply1.getUserId());
                exp1 = forumExpMapper.selectByExample(expExample2).get(0).getExp();
            }
            //点赞状态判断
            ForumReplyPraiseExample replyPraise=new ForumReplyPraiseExample();
            ForumReplyPraiseExample.Criteria criteria4 = replyPraise.createCriteria();
            criteria4.andUserIdEqualTo(userId);
            criteria4.andReplyIdEqualTo(forumReply1.getId());
            int praiseStatus = ForumReplyPraiseMapper.selectByExample(replyPraise).size();
            //点赞量
            ForumReplyPraiseExample replyPraiseExample = new ForumReplyPraiseExample();
            replyPraiseExample.createCriteria().andReplyIdEqualTo(forumReply1.getId());
            long replyPraiseNum = ForumReplyPraiseMapper.countByExample(replyPraiseExample);
            //保存
            Map<String,Object>map=new HashMap<>();
            map.put("replyReply",forumReply1);
            map.put("user",centerUser1);
            map.put("receptionUser",receptionUser);
            map.put("exp",exp1);
            map.put("praiseStatus",praiseStatus);
            map.put("replyPraiseNum",replyPraiseNum);
            replyReply.add(map);
        }
        //保存
        Map<String,Object> data=new HashMap<>();
        data.put("replyReply",replyReply);
        data.put("floorUser",floorUser);
        return data;
    }
    /*
    举报
     */
    @Override
    public Integer addInform(String userId, String topicId, String floorId, String content) {
        try {
            if("null".equals(floorId)){
                //针对话题的举报
                String forumId = forumTopicMapper.selectByPrimaryKey(topicId).getForumId();
                String id = UUID.randomUUID().toString();
                ForumTopicReport topicReport=new ForumTopicReport(id,topicId,userId,content,new Date(),null,1,forumId);
                forumTopicReportMapper.insert(topicReport);
            }else{
                //针对回复的举报
                String forumId = forumReplyMapper.selectByPrimaryKey(floorId).getForumId();
                String id = UUID.randomUUID().toString();
                ForumReplyReport replyReport=new ForumReplyReport(id,floorId,userId,content,new Date(),null,1,forumId);
                forumReplyReportMapper.insert(replyReport);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    /*
    获取话题举报
     */
    @Override
    public PageInfo<Map<String, Object>> topicTnformService(String userId, Integer page, Integer limit, String forumId) {
        //判断是否管理员
        ForumAdminExample adminExample=new ForumAdminExample();
        ForumAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andForumIdEqualTo(forumId);
        int size = forumAdminMapper.selectByExample(adminExample).size();
        if(size==1){
            //是管理员
            ForumTopicReportExample topicReport=new ForumTopicReportExample();
            topicReport.setOrderByClause("date ASC");
            ForumTopicReportExample.Criteria criteria1 = topicReport.createCriteria();
            criteria1 .andForumidEqualTo(forumId);
            criteria1.andStatusEqualTo(1);
            PageHelper.startPage(page,limit);
            List<ForumTopicReport> forumTopicReports = forumTopicReportMapper.selectByExample(topicReport);
            List<Map<String,Object>>data=new ArrayList<>();
            for(ForumTopicReport forumTopicReport:forumTopicReports){
               //举报话题
                ForumTopic forumTopic = forumTopicMapper.selectByPrimaryKey(forumTopicReport.getTopicId());
                //被举报用户
                CenterUser reportUser = centerUserMapper.selectByPrimaryKey(forumTopic.getUserId());
                //用户赋值
                CenterUser user = centerUserMapper.selectByPrimaryKey(forumTopicReport.getUserId());
                Map<String,Object>map=new HashMap<>();
                map.put("user",user);
                map.put("reportUser",reportUser);
                map.put("forumTopic",forumTopic);
                map.put("forumTopicReport",forumTopicReport);
                data.add(map);
            }
            PageInfo<Map<String,Object>>result=new PageInfo<>(data);
            return result;
        }
        return null;
    }
    /*
    话题举报审核提交
     */
    @Override
    @Transactional
    public Integer checkTopicInformService(String userId,String informId, Integer status) {
        try {
            //举报状态更改
            ForumTopicReport forumTopicReport = forumTopicReportMapper.selectByPrimaryKey(informId);
            //判断用户是否是管理员
            ForumAdminExample adminExample=new ForumAdminExample();
            ForumAdminExample.Criteria criteria = adminExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andForumIdEqualTo(forumTopicReport.getForumid());
            int size = forumAdminMapper.selectByExample(adminExample).size();
            if(size!=1){
                return 0;
            }
            if(status==1){
                //审核通过,封贴
                forumTopicReport.setStatus(2);
                forumTopicReport.setAdminId(userId);
                forumTopicReportMapper.updateByPrimaryKey(forumTopicReport);
                //封贴
                ForumTopic forumTopic = forumTopicMapper.selectByPrimaryKey(forumTopicReport.getTopicId());
                forumTopic.setStatus(1);
                forumTopicMapper.updateByPrimaryKey(forumTopic);
            }else{
                //未通过
                forumTopicReport.setStatus(0);
                forumTopicReport.setAdminId(userId);
                forumTopicReportMapper.updateByPrimaryKey(forumTopicReport);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    /*
    回复举报查询
     */
    @Override
    public PageInfo<Map<String, Object>> replyTnformService(String userId, Integer page, Integer limit, String forumId) {
        //判断是否管理员
        ForumAdminExample adminExample=new ForumAdminExample();
        ForumAdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andForumIdEqualTo(forumId);
        int size = forumAdminMapper.selectByExample(adminExample).size();
        if(size==1){
            //是管理员
            ForumReplyReportExample replyReport=new ForumReplyReportExample();
            replyReport.setOrderByClause("date ASC");
            ForumReplyReportExample.Criteria criteria2 = replyReport.createCriteria();
            criteria2.andForumidEqualTo(forumId);
            criteria2.andResultEqualTo(1);
            PageHelper.startPage(page,limit);
            List<ForumReplyReport> forumReplyReports = forumReplyReportMapper.selectByExample(replyReport);
            List<Map<String,Object>>data=new ArrayList<>();
            for(ForumReplyReport forumReplyReport:forumReplyReports){
                //举报回复
                ForumReply forumReply = forumReplyMapper.selectByPrimaryKey(forumReplyReport.getReplyId());
                //被举报用户
                CenterUser reportUser = centerUserMapper.selectByPrimaryKey(forumReply.getUserId());
                //用户赋值
                CenterUser user = centerUserMapper.selectByPrimaryKey(forumReplyReport.getUserId());
                Map<String,Object>map=new HashMap<>();
                map.put("user",user);
                map.put("reportUser",reportUser);
                map.put("forumReply",forumReply);
                map.put("forumReplyReport",forumReplyReport);
                data.add(map);
            }
            PageInfo<Map<String,Object>>result=new PageInfo<>(data);
            return result;
        }
        return null;
    }
    /*
    回复举报提交
     */
    @Override
    @Transactional
    public Integer checkReplyInformService(String userId, String informId, Integer status) {
        try {
            //举报状态更改
            ForumReplyReport forumReplyReport = forumReplyReportMapper.selectByPrimaryKey(informId);
            //判断用户是否是管理员
            ForumAdminExample adminExample=new ForumAdminExample();
            ForumAdminExample.Criteria criteria = adminExample.createCriteria();
            criteria.andUserIdEqualTo(userId);
            criteria.andForumIdEqualTo(forumReplyReport.getForumid());
            int size = forumAdminMapper.selectByExample(adminExample).size();
            if(size!=1){
                return 0;
            }
            if(status==1){
                //审核通过
                forumReplyReport.setResult(2);
                forumReplyReport.setAdminId(userId);
                forumReplyReportMapper.updateByPrimaryKey(forumReplyReport);
                //封贴
                ForumReply forumReply = forumReplyMapper.selectByPrimaryKey(forumReplyReport.getReplyId());
                forumReply.setStatus(1);
                forumReplyMapper.updateByPrimaryKey(forumReply);
            }else{
                //未通过
                forumReplyReport.setResult(0);
                forumReplyReport.setAdminId(userId);
                forumReplyReportMapper.updateByPrimaryKey(forumReplyReport);
            }
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }
    /*
    删除话题
     */
    @Override
    public Integer delTopic(String userId, String delId) {
        //判断是否本人操作
        ForumTopic forumTopic = forumTopicMapper.selectByPrimaryKey(delId);
        if(forumTopic==null){
            return 0;
        }
        String userId1 =forumTopic.getUserId();
        if(userId.equals(userId1)){
            //是本人操作
            forumTopic.setStatus(2);
            forumTopicMapper.updateByPrimaryKey(forumTopic);
            return 1;
        }else {
            return 0;
        }
    }
    /*
    删除收藏
     */
    @Override
    public Integer delConcern(String userId, String delId) {
        //获取该收藏
        ForumTopicCollectExample topicCollect=new ForumTopicCollectExample();
        ForumTopicCollectExample.Criteria criteria1 = topicCollect.createCriteria();
        criteria1.andUserIdEqualTo(userId);
        criteria1.andTopicIdEqualTo(delId);
        List<ForumTopicCollect> forumTopicCollects = forumTopicCollectMapper.selectByExample(topicCollect);
        if(forumTopicCollects.size()!=0){
            //收藏存在，删除
            forumTopicCollectMapper.deleteByPrimaryKey(forumTopicCollects.get(0).getId());
        }
        return 1;
    }
}
